#include "mt.h"


int main(int argc, char* argv[])
{
	int sock;
	
	/*Valido argumentos*/	
	if (argc != 2)
	{
		printf("Error: Debe ingresar la direccion IP. \n");
		exit(1);
	}	

	struct sockaddr_in name;
	struct in_addr ad;

	name.sin_family = AF_INET;
	name.sin_addr.s_addr = INADDR_ANY;
	name.sin_port = htons(PORT);
	name.sin_addr = ad;

	/*Recupero la ip*/
	char* ipChar = argv[1];
	int res = inet_aton(ipChar, &name.sin_addr);

	if (res == 0 )
	{
		printf("Error recuperando ip");
		exit(1);
	}

	/* Crear socket sobre el que se lee: dominio INET, protocolo UDP (DGRAM). */
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock < 0) {
		perror("Error abriendo socket");
		exit(1);
	}
	
	char buf[MAX_MSG_LENGTH];
	/*Ingreso comando para enviar al servidor hasta ingresar END_STRING */
	for(;;)
	{
		fgets(buf, MAX_MSG_LENGTH, stdin);
		
		printf("Comando: %s", buf);		

		if (sendto(sock, buf, sizeof(buf), 0, (struct sockaddr*)&name, sizeof(name)) != sizeof(buf))
		{
			printf("No se pudo enviar el texto.");
		}
		
		if (!strncmp(buf, END_STRING, MAX_MSG_LENGTH))
		{
			break;
		}	
	
	}

	
	return 0;
}
